package com.crm.services;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.crm.model.jdao.IOutgoodDao;
import com.crm.model.mdao.CustomerMapper;
import com.crm.model.mdao.OrderInfoMapper;
import com.crm.model.mdao.OrderitemsMapper;
import com.crm.model.mdao.OutgoodMapper;
import com.crm.model.mdao.OutgooddetailsMapper;
import com.crm.model.mpojo.Orderitems;
import com.crm.model.mpojo.Outgooddetails;
import com.crm.model.pojo.Customer;
import com.crm.model.pojo.Orderinfo;
import com.crm.model.pojo.Outgood;
import com.crm.model.pojo.Timeline;
import com.github.pagehelper.PageHelper;

@Service
@Transactional(rollbackFor = Exception.class)
public class OutgoodService {
	
	@Autowired
	OrderInfoServers orderServer;
	@Autowired
	OrderInfoMapper orderMapper;
	
	@Autowired
	CustomerServices cumService;
	@Autowired
	OutgoodMapper outgoodMapper;
	
	@Autowired
	IOutgoodDao iOutgoodDao;
	
	@Autowired
	OrderitemsMapper oitemsMapper;
	@Autowired
	OutgooddetailsMapper outgooddetailsMapper;
	@Autowired
	TimeLineServices timeLineServices;
	
	@Autowired
	CustomerMapper customerMapper;
	
	/**
	 * 分页查询
	 * 
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	public List<Map<String, String>> selectByPager(int pageNum, int pageSize,Integer statusid,String search) {
		// 使用分页插件
		System.out.println("pageNum:" + pageNum + ",pageSize:" + pageSize);
		// 配置分页信息。这句话后面的第一个查询将调用分页。使用分页，对应的Service方法最好只写一个查询方法
		PageHelper.startPage(pageNum, pageSize);
		Integer dstatus=null;
		
		if(statusid!=null) {
		if(statusid==0||statusid==1)
			dstatus=statusid;
		}
		System.out.println("查询值"+dstatus);
		// 获取
		List<Map<String, String>> list = outgoodMapper.selectAlls(dstatus,search);
		System.out.println(list);
		System.out.println("大小" + list.size());
		return list ;
	}
	
	
	/**
	 * 订单视图添加发货单
	 * @param id
	 * @return
	 */
	public Integer addorderitemview(Integer id) {
		com.crm.model.mpojo.Orderinfo order1 = orderMapper .selectByPrimaryKey(id);
		Orderinfo orderinfo = orderServer.findById(id);
		Customer customer = cumService.findByCustIdPage(order1.getCustId());
		Outgood outgood=new Outgood();
		outgood.setCustomer(customer);
		outgood.setOrderinfo(orderinfo);
		outgood.setConsigner(orderinfo.getUId()+"");
		outgood.setShoumen(orderinfo.getOConsignee());
		outgood.setOgAddress(orderinfo.getOAddress());
		outgood.setOgPostcode(orderinfo.getOPostcode());
		System.out.println("电话："+orderinfo.getoConphone());
		outgood.setOgPhone(orderinfo.getoConphone());
		outgood.setOgStatus(1);
		outgood.setOgType(0);
		Outgood save = iOutgoodDao.save(outgood);
		//遍历订单明细
		List<Orderitems> selectByoId = oitemsMapper.selectByoId(id);
		for (Orderitems orderitems : selectByoId) {
			Outgooddetails outgooddetails = new Outgooddetails(); 
			outgooddetails.setpId(orderitems.getpId());
			outgooddetails.setOgId(save.getOgId());
			outgooddetails.setOdPrice(orderitems.getOiPrice());
			outgooddetails.setOdCount(orderitems.getOiCount());
			outgooddetails.setOdDiscount(orderitems.getOiDiscount());
			outgooddetails.setOdMoney(orderitems.getOiMoney());
			outgooddetails.setOdRemark(orderitems.getOiRemark());
			outgooddetails.setOdStatue(1);
			outgooddetailsMapper.insertSelective(outgooddetails);
		}
		System.out.println("订单"+outgood);
		System.out.println("订单明细" +selectByoId);
		return save.getOgId();
	}
	
	/**
	 * 编辑物流信息完成发货
	 */
	public void delivergoods(com.crm.model.mpojo.Outgood outgood,Integer oId) {
		outgood.setDeliverydate(new Date());
		outgood.setOgType(1);
		outgood.setOgDeliverymethod(1445);
		outgoodMapper.updateByPrimaryKeySelective(outgood);
		orderMapper.selectByPrimaryKey(oId);
		//订单状态修改为已完成
		com.crm.model.mpojo.Orderinfo order=new com.crm.model.mpojo.Orderinfo();
		order.setoId(oId);
		order.setoFadate(new Date());
		order.setoDstatus(1100);
		order.setoShipments(1092);
		com.crm.model.mpojo.Orderinfo orde = orderMapper.selectByPrimaryKey(oId);
		
		//加入时间线
		Timeline timeline=new Timeline();
		timeline.setTheme("订单发货");
		timeline.setTimes(new Date());
		timeline.setCustId(orde.getCustId());
		timeline.setDetails("订单号"+oId+"已完成发货");
		timeLineServices.insertTimeLine(timeline);
		//修改客户类型为二次购买
		com.crm.model.mpojo.Customer cust=new com.crm.model.mpojo.Customer();
		cust.setCustId(outgood.getCustId());
		cust.setCustSort(1112);
		customerMapper.updateByPrimaryKeySelective(cust);
		orderMapper.updateByPrimaryKeySelective(order);
	}
	
	/**
	 * 编辑物流信息完成发货
	 */
	public void delivergood(com.crm.model.mpojo.Outgood outgood) {
		outgood.setOgDeliverymethod(1445);
		outgoodMapper.updateByPrimaryKeySelective(outgood);
	}
	/**
	 * 编辑地址信息完成发货
	 */
	public void delivergoodAddress(com.crm.model.mpojo.Outgood outgood) {
		outgoodMapper.updateByPrimaryKeySelective(outgood);
	}
}
